<?php

class ModuleDao extends Dao {
	
    function __construct() {
        $this->table = 'module';
    }
    
    public function insertObject($obj){
            $result = DB::insert(DB::raw("INSERT INTO module (id, name, sequence, icon, is_enabled, is_show, created_at) ".
                    "VALUES ('".$obj->getId()."' ".
                            ", '".$obj->getName()."' ".
                            ", '".$obj->getSequence()."' ".
                            ", '".$obj->getIcon()."' ".
                            ", '".$obj->getIsEnabled()."' ".
                            ", '".$obj->getIsShow()."' ".
                            ", null ".
                            ")"));
            if (!$result) { return null; }

            return $obj->getId();//DB::getPdo()->lastInsertId();
    }

    public function updateObject($obj, $old_id){
        DB::update(DB::raw("UPDATE module SET id='".$obj->getId()."'"
                . ", name='".$obj->getName()."'"
                . ", sequence='".$obj->getSequence()."'"
                . ", icon='".$obj->getIcon()."'"
                . ", is_enabled=".$obj->getIsEnabled().""
                . ", is_show=".$obj->getIsShow().""
                . ", updated_at=null"
                . " WHERE id='".$old_id."'"));
    
        return true;
    }
    
    public function deleteObject($id){
        DB::delete(DB::raw("DELETE FROM module where id='".$id."'"));
        
        return true;
    }
    
    public function getObject($id){
        $result = DB::select(DB::raw("SELECT * FROM module where id='".$id."'"));
        
        if (!$result) { return null; }
        return $this->toObject($result[0]);
    }
    
    public function getList($criteria = NULL){
        $result = DB::select(DB::raw("SELECT * FROM module"));
        $list = array();
        foreach ($result as $rowdata){
            $list[] = $this->toObject($rowdata);
        }
        return $list;
    }
    
    protected function toObject($rowdata=null) {
        $module = new Module();        
        $module->setId($rowdata->id);
        $module->setName($rowdata->name);
        $module->setIcon($rowdata->icon);
        $module->setSequence($rowdata->sequence);
        $module->setIsShow($rowdata->is_show);
        $module->setIsEnabled($rowdata->is_enabled);
        $module->setIsLoaded(true);

        return $module;
    }
            
}
